//
//  MainModel.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "MainModel.h"

@implementation MainModel

+ (void)privateRequestWithCompanyName:(NSString *)companyName
                             userName:(NSString *)userName
                                phone:(NSString *)phone
                              problem:(NSString *)problem
                          rewardValue:(NSString *)rewardValue
                                 time:(NSString *)time
                             location:(NSString *)location
                         locationDesc:(NSString *)locationDesc
                           completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  MainInterface *interface = [[MainInterface alloc] init];
  [interface privateRequestWithCompanyName:companyName userName:userName phone:phone problem:problem rewardValue:rewardValue time:time location:location locationDesc:locationDesc completion:_completion error:_error];
}

@end

@implementation BannerModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"bannerid" }];
}

//获取banner列表
+ (void)queryBannerListWithCompletion:(void (^)(NSMutableArray *bannerList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          BannerModel *bannerModel = [[BannerModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:bannerModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  MainInterface *interface = [[MainInterface alloc] init];
  [interface queryBannerListWithCompletion:_completion error:_error];
}

@end

@implementation BannerData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation LocationModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"locid" }];
}

//获取地区列表
+ (void)queryLocationListWithCompletion:(void (^)(NSMutableArray *locList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          LocationModel *locModel = [[LocationModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:locModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  MainInterface *interface = [[MainInterface alloc] init];
  [interface queryLocationListWithCompletion:_completion error:_error];
}

@end

@implementation LocationData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end
